package com.fh.db.oracle;

import com.fh.db.*;
import com.fh.db.base.BaseDbSchema;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author qiujunda
 * @Date 16/05/2020
 * @Description
 */
public class OracleDbSchema extends BaseDbSchema {


    @Override
    public List<DbTable> getTables() {
        return (List<DbTable>) SqlRunner.preparedSqlResult(dbSource, dbSource.getSqlSet().tablePreSql(), new String[]{},
                resultSet -> {
                    List<DbTable> result = new ArrayList<>();
                    try {
                        while (resultSet.next()) {
                            OracleDbTable oracleDbTable = new OracleDbTable();
                            oracleDbTable.setDbSource(dbSource);
                            oracleDbTable.setTableName(resultSet.getString("TABLE_NAME"));
                            oracleDbTable.setOwner(resultSet.getString("OWNER"));
                            oracleDbTable.setTablespaceName(resultSet.getString("TABLESPACE_NAME"));
                            oracleDbTable.setTableComments(resultSet.getString("COMMENTS"));
                            BigDecimal numRows = resultSet.getBigDecimal("NUM_ROWS");
                            oracleDbTable.setRowNum(numRows == null ? null : numRows.intValue());
                            oracleDbTable.setCreated(resultSet.getTimestamp("CREATED"));
                            oracleDbTable.setLastDdlTime(resultSet.getTimestamp("LAST_DDL_TIME"));
                            result.add(oracleDbTable);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return result;
                });

    }

    @Override
    public List<DbView> getViews() {
        return (List<DbView>) SqlRunner.preparedSqlResult(dbSource, dbSource.getSqlSet().viewPreSql(), new String[]{dbSource.getUserName()},
                resultSet -> {
                    List<DbView> result = new ArrayList<>();
                    try {
                        while (resultSet.next()) {
                            OracleDbView dbView = new OracleDbView();
                            dbView.setViewName(resultSet.getString("VIEW_NAME"));
                            dbView.setTextLength(resultSet.getBigDecimal("TEXT_LENGTH").intValue());
                            dbView.setTextContent(SqlRunner.convertOracleLongColumn(resultSet.getBinaryStream("TEXT")));
                            dbView.setCreated(resultSet.getTimestamp("CREATED"));
                            dbView.setLastDdlTime(resultSet.getTimestamp("LAST_DDL_TIME"));
                            result.add(dbView);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return result;
                });
    }

    @Override
    public List<DbFunction> getFunctions() {
        return null;
    }

    @Override
    public List<DbProcedure> getProcedures() {
        return null;
    }

    @Override
    public List<DbPackage> getPackages() {
        return (List<DbPackage>) SqlRunner.preparedSqlResult(dbSource, dbSource.getSqlSet().packagePreSql(), new String[]{},
                resultSet -> {
                    List<DbPackage> result = new ArrayList<>();
                    try {
                        while (resultSet.next()) {
                            OracleDbPackage oracleDbPackage = new OracleDbPackage();
                            oracleDbPackage.setDbSource(dbSource);
                            oracleDbPackage.setOwner(resultSet.getString("OWNER"));
                            oracleDbPackage.setPackageName(resultSet.getString("OBJECT_NAME"));
                            oracleDbPackage.setCreated(resultSet.getTimestamp("CREATED"));
                            oracleDbPackage.setPackageLastDdlTime(resultSet.getTimestamp("P_LAST_DDL_TIME"));
                            oracleDbPackage.setBodyLastDdlTime(resultSet.getTimestamp("B_LAST_DDL_TIME"));
                            result.add(oracleDbPackage);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return result;
                });
    }

    @Override
    public DbSource getDbSource() {
        return this.dbSource;
    }

    public void setDbSource(DbSource dbSource) {
        this.dbSource = dbSource;
    }
}
